const consola = require('consola')
const config = require('../config/config')
const mongoose = require('mongoose')
const autoIncrement = require('mongoose-auto-increment')
//避免mongoose报错
mongoose.set('useFindAndModify',false)
mongoose.Promise=global.Promise
exports.mongoose=mongoose
exports.connect=()=>{
    //连接数据库
    mongoose.connect(config.MONGODB.uri,{
        useCreateIndex:true,
        useNewUrlParser:true,
        promiseLibrary:global.Promise
    })
    //连接失败
    mongoose.connection.on('error',error=>{
        consola.warn('数据库连接失败',error)
    })
    // 连接成功
    mongoose.connection.once('open',()=>{
        consola.ready('数据库连接成功')
    })
    // 自增id初始化
    autoIncrement.initialize(mongoose)
    return mongoose
}